package com.reder.practice.common.filter;

import com.reder.practice.common.util.SessionUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;

/**
 * 调试使用
 *
 * @author Fish
 * ------> 1149062639@qq.com
 * created by 2018/11/12 23:30:59
 */
@WebFilter(urlPatterns = {
        "/*"
})
@Slf4j
public class DebugFilter implements Filter {

    // 是否开启 DEBUG 模式, 默认false
    private volatile static boolean debugMode = false;

    // 开启 DEBUG 模式
    public synchronized static boolean turnOnDebugMode() {
        DebugFilter.debugMode = true;
        return DebugFilter.debugMode;
    }

    // 关闭 DEBUG 模式
    public synchronized static boolean turnOffDebugMode() {
        DebugFilter.debugMode = false;
        return DebugFilter.debugMode;
    }

    // 获取 DEBUG 模式
    public synchronized static boolean isDebugMode() {
        return debugMode;
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        // 开启 DEBUG 模式才输出
        if (isDebugMode()) {
            printInfoOfRequest((HttpServletRequest) req);
            printInfoOfSession((HttpServletRequest) req);
        }
        chain.doFilter(req, resp);
    }

    // 打印请求信息
    private void printInfoOfRequest(HttpServletRequest request) {

        log.debug("=========================== 请求信息 =========================");
        printUrlOfRequest(request);
        printHeadersOfRequest(request);
        printParamsOfRequest(request);
        log.debug("=============================================================");
    }

    // 打印请求路径
    private void printUrlOfRequest(HttpServletRequest request) {
        log.debug("请求路径：");
        log.debug("\tURL : " + request.getRequestURL().toString());
        log.debug("\tURI : " + request.getRequestURI());
    }

    // 打印请求头
    private void printHeadersOfRequest(HttpServletRequest request) {
        log.debug("请求头：");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String nameOfHeader = headerNames.nextElement();
            log.debug("\t" + nameOfHeader + " : " + request.getHeader(nameOfHeader));
            log.debug("\t\t使用 getHeaders 获取：");
            Enumeration<String> headers = request.getHeaders(nameOfHeader);
            while (headers.hasMoreElements()) {
                log.debug("\t\t\t" + headers.nextElement());
            }
        }
    }

    // 打印请求参数
    private void printParamsOfRequest(HttpServletRequest request) {
        log.debug("请求参数：");
        Map<String, String[]> parameters = request.getParameterMap();
        parameters.forEach((k, v) -> {
            log.debug("\t" + k + " : " + Arrays.toString(v));
        });
    }

    // 打印会话信息
    private void printInfoOfSession(HttpServletRequest request) {

        log.debug("=========================== 会话信息 =========================");
        log.debug("是否有登录：" + SessionUtil.hasLogin(request));
        log.debug("登录用户：" + SessionUtil.loginObjectOf(request));
        log.debug("=============================================================");
    }
}
